<?php

function request($k, $t = 'R',$default = null) {
	switch($t) {
		case 'P': $var = &$_POST; break;
		case 'G': $var = &$_GET; break;
		case 'C': $var = &$_COOKIE; break;
		case 'R': $var = &$_REQUEST; break;
	}
	return isset($var[$k]) ? (is_array($var[$k]) ? $var[$k] : trim($var[$k])) : $default;
}
function config_item($key){
    return RouterCore::getInstance()->getConf($key);
}
if ( ! function_exists('remove_invisible_characters'))
{
    function remove_invisible_characters($str, $url_encoded = TRUE)
    {
        $non_displayables = array();

        // every control character except newline (dec 10)
        // carriage return (dec 13), and horizontal tab (dec 09)

        if ($url_encoded)
        {
            $non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
            $non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
        }

        $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127

        do
        {
            $str = preg_replace($non_displayables, '', $str, -1, $count);
        }
        while ($count);

        return $str;
    }
}

function getNumber($k,$min = 0){
	$number = request($k,'R',$min);
	$number = $number < $min ? $min : $number;
	return $number;
}

function getPage($k='page'){
	$page = request($k,'G',1);
	$page = $page < 1 ? 1 : $page;
	return $page;
}

function timeIsNull($timeStr){
	return $timeStr == null || $timeStr == "" || $timeStr == "0000-00-00" || $timeStr == "0000-00-00 00:00:00";
}

function formatDateTime($timeStr){
	return strlen($timeStr) > 10 ? substr($timeStr, 0,10) : $timeStr;
}

function getPageString($total,$size,$page,$url=''){
	$sp = $url && strpos($url, "?") > -1 ? "&" : "?";
	$total = ceil($total / $size);
	if($total < 1){
		return "";
	}
	$pagestr =  '<a href="'.$url.$sp.'page=1" class="padding bold">First</a>';
	for ($i = 1; $i <= $total; $i++) {
		$pagestr .= '<a href="'.($i == $page ? 'javascript:void(0);' : $url.$sp.'page='.$i).'"'.($i == $page ? ' class="active"' : '').'>'.$i.'</a>';
	}
	$pagestr .=  '<a href="'.$url.$sp.'page='.$total.'" class="padding bold">Last</a>';
	return $pagestr;
}

function getClientIP()
{
	global $ip;
	if (getenv("HTTP_CLIENT_IP")){
		$ip = getenv("HTTP_CLIENT_IP");
	}else if(getenv("HTTP_X_FORWARDED_FOR")){
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	}else if(getenv("REMOTE_ADDR")){
		$ip = getenv("REMOTE_ADDR");
	}else{
		$ip = "Unknow";
	}
	return $ip;
}

function getString2Time($str){
	$year=((int)substr($str,0,4));//取得年份
	$month=((int)substr($str,5,2));//取得月份
	$day=((int)substr($str,8,2));//取得几号
	$hours = 23;
	$minute = 59;
	$second = 59;
	if(strlen($str) != 19){
		$hours = ((int)substr($str,11,2));//取得小时
		$minute = ((int)substr($str,14,2));//取得分钟
		$second = ((int)substr($str,17,2));//取得秒
	}
	return mktime($hours,$minute,$second,$month,$day,$year);
}
function dateformat($datetime,$format = "Y-m-d H:i:s"){
	if(strlen($datetime) != 19){
		return $datetime;
	}
	$year = ((int)substr($datetime,0,4));//取得年份
	$month = ((int)substr($datetime,5,2));//取得月份
	$day = ((int)substr($datetime,8,2));//取得几号
	$hours = ((int)substr($datetime,11,2));//取得小时
	$minute = ((int)substr($datetime,14,2));//取得分钟
	$second = ((int)substr($datetime,17,2));//取得秒
	return date($format,mktime($hours,$minute,$second,$month,$day,$year));
}

function ajaxSuccess($data = ''){
    ajaxResponse(0,'操作成功',$data);
}
function ajaxResponse($code = 0,$msg = '操作成功',$data = ''){
    if(is_array($code)){
        $data = $msg;
        $msg = $code[1];
        $code = $code[0];
    }
	exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data)));
}
function isAjax(){
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
        strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
        return true;
    }
    return false;
}

function getPassword($loginId,$loginPass){
	$pass = '$P$';
	$pass1 = base64_encode(md5($loginId.$loginPass));
	$pass2 = '$'.base64_encode(time());
	return str_replace('=', '', strtoupper($pass.$pass1.$pass2));
}

function getPassword1($loginId,$loginPass){
    $pass = '$P$';
    $pass1 = base64_encode(md5($loginId.$loginPass));
    $pass2 = '$'.substr(md5($loginId.$loginPass),0,4);
    return str_replace('=', '', strtoupper($pass.$pass1.$pass2));
}

function str_endwith($str,$search){
	return preg_match('['.$str.'$]', $search);
	if(substr($str, 0,0-strlen($search)) == $search){
		return true;
	}
	return false;
}

function str_startwith($str,$search){
	return preg_match('[^'.$str.']', $search);
}

/**
 * 获取文件名后缀
 */
function getFileSuffix($fileName) {
	return strtolower(pathinfo($fileName,  PATHINFO_EXTENSION));
}

function getFileName($filePath){
	if(strpos($filePath, "/")){
		return substr($filePath,lastindexof($filePath, "/")+1);
	}
	return $filePath;
}

function getFileNameWithOutSuffix($filePath){
    $fileName = getFileName($filePath);
    $suffixLen = strlen(getFileSuffix($fileName));
    if($suffixLen > 0){
        $suffixLen ++;
    }
    return substr($fileName,0,strlen($fileName) - $suffixLen);
}
function lastindexof($all,$part){
	if(trim($all)=="" || trim($part)=="") return 0;
	$lastindexof = $offset = 0;
	while(strpos($all,$part)!==false)
	{
		$indexof = strpos($all,$part);
		$lastindexof = $lastindexof + $indexof + $offset;
		$all = substr($all,$indexof+ strlen($part));
		$offset = strlen($part);
	}

	return $lastindexof;
}

function getDirFiles($dirpath,$filter = ''){
	$dir = opendir($dirpath);
	$files = array();
	while (($file = readdir($dir)) !== false)
	{
		if(!str_startwith($file, '.') && !str_startwith($file, '..')){
			if($filter){
				if(preg_match('/'.$filter.'/', $file)){
					$files[] = $file;
				}
			}else{
				$files[] = $file;
			}
		}
	}
	closedir($dir);
	return $files;
}

/**
 * Makes directory
 * @link http://www.php.net/manual/en/function.mkdir.php
 * @param dir string <p>
 * The directory path.
 * </p>
 */
function mkdirs($dir)
{
	if(!is_dir($dir))
	{
		if(!mkdirs(dirname($dir))){
			return false;
		}
		if(!mkdir($dir,0777)){
			return false;
		}
	}
	return true;
}
/**
 * 删除目录
 * @param string $dirName
 */
function delDirAndFile( $dirName )
{
	if ( $handle = opendir( "$dirName" ) ) {
		while ( false !== ( $item = readdir( $handle ) ) ) {
			if ( $item != "." && $item != ".." ) {
	   if ( is_dir( "$dirName/$item" ) ) {
	   	delDirAndFile( "$dirName/$item" );
	   } else {
	   	if( unlink( "$dirName/$item" ) ){
	   		return true;
	   	}
	   }
			}
		}
		closedir( $handle );
		if(  rmdir( $dirName )){
			return true;
		}
	}
}

function base62($x) 
{ 
    $show = ""; 
    while($x > 0) { 
        $s = $x % 62; 
        if ($s > 35) { 
            $s = chr($s+61);             
        } elseif ($s > 9 && $s <=35) { 
            $s = chr($s + 55); 
        } 
        $show .= $s; 
        $x = floor($x/62); 
    } 
    return $show;     
} 
 
/**
 * 短网址生成
 * Enter description here ...
 * @param unknown_type $url
 */
function urlShort($url) 
{ 
    $url = crc32($url); 
    $result = sprintf("%u", $url); 
    $md5= substr(md5($url), 0,6);
    return base62($result).$md5; 
}

function shortUrl($long_url)
{
	$key = 'xiaoyan.me';
	$base32 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	// 利用md5算法方式生成hash值
	$hex = hash('md5', $long_url.$key);
	$hexLen = strlen($hex);
	$subHexLen = $hexLen / 8;

	$output = array();
	for( $i = 0; $i < $subHexLen; $i++ )
	{
		// 将这32位分成四份，每一份8个字符，将其视作16进制串与0x3fffffff(30位1)与操作
		$subHex = substr($hex, $i*8, 8);
		$idx = 0x3FFFFFFF & (1 * ('0x' . $subHex));

		// 这30位分成6段, 每5个一组，算出其整数值，然后映射到我们准备的62个字符
		$out = '';
		for( $j = 0; $j < 6; $j++ )
		{
			$val = 0x0000003D & $idx;
			$out .= $base32[$val];
			$idx = $idx >> 5;
		}
		$output[$i] = $out;
	}
	return $output[2].$output[3];
}


function showGravatar($email, $size=50)
{
    echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).'&size='.$size.'" width="'.$size.'" height="'.$size.'" style="vertical-align: middle;" />';
}

function get_long_time($date){
    $curr = time();
    $tmp = $curr - $date;
    if($tmp < 60){
        $re = $tmp.'秒前';
    }else if($tmp < 3600){
        $re = floor($tmp/60).'分钟前';
    }else if($tmp < 86400){
        $re = floor($tmp/3600).'小时前';
    }else if($tmp < 259200){//3天内
        $re = floor($tmp/86400).'天前';
    }else{
        $re = date('Y年m月d日 H:i:s',$date);
    }
    echo $re;
}

function DB($config = array()){
    $DB_KEY = 'DB_OBJ';
    if(Cache::getInstance()->exists($DB_KEY)){
        return Cache::getInstance()->get($DB_KEY);
    }
    $dbc = RouterCore::getInstance()->getConf('db_config');
    if(!empty($config)) $dbc = $config;
    $db_obj = new DBCore($dbc);
    Cache::getInstance()->set($DB_KEY,$db_obj);
    return $db_obj;
}


function THEME(){
    $r = RouterCore::getInstance();
    return URL().'/static/template/'.$r->getConf('theme');
}
function URL(){
    return '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
}

function initMore(){
    $r = RouterCore::getInstance();
    $twig = $r->getTwig();
    $path = $twig->getLoader()->getPaths();
    $twig->getLoader()->setPaths($path[0].'/'.$r->getConf('theme'));
    $twig->addFunction("__THEME__",new Twig_Function_Function("THEME"));
    $twig->addFunction("__URL__",new Twig_Function_Function("URL"));
    $r->getTmpl()->assign('__URL__',URL());
    $r->getTmpl()->assign('__THEME__',THEME());
}
function buildTree($items,$key='id',$parentKey = 'parent') {
    $newData = array();
    foreach($items as $item)
        $newData[$item[$key]] = $item;
    $items = $newData;
    foreach ($items as $item)
        $items[$item[$parentKey]]['child'][$item[$key]] = &$items[$item[$key]];
    return isset($items[0]['child']) ? $items[0]['child'] : array();
}

function createUnique($prefix='TSK') {
    $data = substr(uniqid(),2).rand(10,99);
    return strtoupper($prefix.$data);
}

/**
 * @param $fileName
 * @return if file not exists return false else if can read return array or return null
 */
function getExcelData($fileName)
{
    if (!file_exists($fileName)) {
        return false;
    }
    YCLoader::init()->import('Excel/PHPExcel');
    $fileInfo = pathinfo($fileName);
    $excel = new PHPExcel();

    /**默认用excel2007读取excel，若格式不对，则用之前的版本进行读取*/
    $reader = new PHPExcel_Reader_Excel2007();
    if (strtolower($fileInfo['extension']) == 'xls') {
        $reader = new PHPExcel_Reader_Excel5();
    }
    if (!$reader->canRead($fileName)) { //判断能否读取数据
        return null;
    }
    $excel = $reader->load($fileName); //加载文件
    $sheet = $excel->getActiveSheet(); //获取活动的sheet
    $highestRow = $sheet->getHighestRow(); //获得最大行数
    $highestColumn = $sheet->getHighestColumn(); //获得最大列数
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $excelData = array();
    for ($row = 1; $row <= $highestRow; $row++) {
        for ($col = 0; $col < $highestColumnIndex; $col++) {
            $excelData[$row][] = (string)$sheet->getCellByColumnAndRow($col, $row)->getValue();
        }
    }
    return $excelData;
}

function array_key_values($array,$key){
    $arr = array();
    foreach($array as $v){
        $arr[] = $v[$key];
    }
    return $arr;
}
function array_value_toKey($array,$key){
    $arr = array();
    foreach($array as $v){
        if(!is_array($v[$key])) $arr[$v[$key]] = $v;
    }
    return $arr;
}